﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_AddClass]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_AddClass];
GO
CREATE PROCEDURE [dbo].[sproc_AddClass]
        @ClassName nvarchar(300),
        @ClassRemark nvarchar(300),
        @ClassType int,
        @ClassParentID int,
        @AddedBy nvarchar(300),
        @AddedDate datetime,
        @Status int
--添加类节点
AS
--父接点小于等于零时，为跟接点
SET NOCOUNT ON
IF @ClassParentID<=0
BEGIN
    INSERT INTO uds_class (classname,classremark,classtype,classparentid,classaddedby,classaddeddate,status)
        VALUES(@ClassName,@ClassRemark,@ClassType,@ClassParentID,@AddedBy,@AddedDate,@Status)
    UPDATE uds_class SET classparentid = classid 
        WHERE classid = @@IDENTITY 
    SELECT @@IDENTITY AS CLASSID
    RETURN @@IDENTITY
END
ELSE
BEGIN
    --判断是否存在父接点
    IF EXISTS(SELECT 1 FROM uds_class WHERE classid = @classparentid)
    BEGIN
        INSERT INTO uds_class (classname,classremark,classtype,classparentid,classaddedby,classaddeddate,status)
            VALUES(@ClassName,@ClassRemark,@ClassType,@ClassParentID,@AddedBy,@AddedDate,@Status)
        SELECT @@IDENTITY AS CLASSID 
        RETURN @@IDENTITY
    END
    ELSE
        SELECT 0 AS CLASSID
        RETURN 0
END    
SET NOCOUNT OFF